import java.util.Scanner;

public class ObserverPatternModule {
    private final Scanner scanner = new Scanner(System.in);
    private final SoilMoistureSensor soilSensor = new SoilMoistureSensor();
    
    public ObserverPatternModule() {
        // 注册观察者
        soilSensor.addObserver(new IrrigationSystem());
    }
    
    public void execute() {
        while (true) {
            System.out.println("\n===== 农田环境监测 =====");
            System.out.println("1. 设置土壤湿度");
            System.out.println("2. 查看当前湿度");
            System.out.println("3. 返回主菜单");
            System.out.print("请选择操作(1-3): ");
            
            int choice = scanner.nextInt();
            scanner.nextLine();  // 消耗换行符
            
            switch (choice) {
                case 1 -> setSoilMoisture();
                case 2 -> viewSoilMoisture();
                case 3 -> { return; }
                default -> System.out.println("无效选择，请重新输入");
            }
        }
    }
    
    private void setSoilMoisture() {
        System.out.print("请输入土壤湿度(%): ");
        double moisture = scanner.nextDouble();
        soilSensor.setMoisture(moisture);
    }
    
    private void viewSoilMoisture() {
        System.out.println("当前土壤湿度: " + soilSensor.getMoisture() + "%");
    }
}